#include <stdio.h>
#include "../IMultInterface/IMultInterface.h"

void main()
{
    HRESULT hr;

    hr = CoInitialize(NULL);
    if (FAILED(hr))
    {
        fprintf(stderr, "CoInitialize failed with error code: %x\n", hr);
        return;
    }

    IBase *pBase = NULL;
    hr = CoCreateInstance(CLSID_IMultInterface, NULL, CLSCTX_INPROC_SERVER, IID_IBase, (LPVOID *)&pBase);
    if (FAILED(hr))
    {
        fprintf(stderr, "CoCreateInstance failed with error code: %x\n", hr);
        CoUninitialize();
        return;
    }

    LONG r;
    hr = pBase->Sum(1, 2, &r);
    if (FAILED(hr))
    {
        fprintf(stderr, "Sum failed with error code: %x\n", hr);
        pBase->Release();
        CoUninitialize();
        return;
    }
    printf("Sum: %d\n", r);

    ISub1 *pSub1 = NULL;
    hr = pBase->QueryInterface(IID_ISub1, (void **)&pSub1);
    pBase->Release();
    pBase = NULL;
    if (FAILED(hr))
    {
        fprintf(stderr, "QueryInterface failed with error code: %x\n", hr);
        CoUninitialize();
        return;
    }

    hr = pSub1->ShowMessage(L"hello, world!");
    if (FAILED(hr))
    {
        fprintf(stderr, "ShowMessage failed with error code: %x\n", hr);
        pSub1->Release();
        CoUninitialize();
        return;
    }

    ISub2 *pSub2 = NULL;
    hr = pSub1->QueryInterface(IID_ISub2, (void **)&pSub2);
    pSub1->Release();
    pSub1 = NULL;
    if (FAILED(hr))
    {
        fprintf(stderr, "QueryInterface failed with error code: %x\n", hr);
        CoUninitialize();
        return;
    }

    hr = pSub2->Increment();
    if (FAILED(hr))
    {
        fprintf(stderr, "Increment failed with error code: %x\n", hr);
        pSub2->Release();
        CoUninitialize();
        return;
    }

    LONG v;
    hr = pSub2->GetValue(&v);
    if (FAILED(hr))
    {
        fprintf(stderr, "GetValue failed with error code: %x\n", hr);
        pSub2->Release();
        CoUninitialize();
        return;
    }
    printf("Value: %d\n", v);

    pSub2->Release();
    CoUninitialize();
}